package com.ruoyi.oss.cloud;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import com.aliyun.oss.OSSClient;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oss.utils.ImageCheck;

public class AliyunCloudStorageService extends AbstractCloudStorageService {

    public AliyunCloudStorageService(CloudStorageConfig config){
        this.config = config;
    }

    @Override
    public String upload(byte[] data, String path) {
        return upload(new ByteArrayInputStream(data), path);
    }

    @Override
    public String upload(InputStream inputStream, String path) {
        OSSClient client = new OSSClient(config.getAliyunEndPoint(), config.getAliyunAccessKeyId(),
                config.getAliyunAccessKeySecret());
        try {
            client.putObject(config.getAliyunBucketName(), path, inputStream);
            client.shutdown();
        } catch (Exception e){
        	throw new ServiceException(e.getMessage());
        }

        String url = config.getAliyunDomain() + "/" + path;
        return url;
    }

    @Override
    public String uploadSuffix(byte[] data, String suffix) {
        return upload(data, getPath(config.getAliyunPrefix(), suffix));
    }

    @Override
    public String uploadSuffix(InputStream inputStream, String suffix) {
        return upload(inputStream, getPath(config.getAliyunPrefix(), suffix));
    }

	@Override
	public String check(String url) {
		return ImageCheck.check(url, "qrcode", config.getAliyunAccessKeyId(), config.getAliyunAccessKeySecret());
	}
}
